package com.jiaok.config.securityHandler;

import com.jiaok.entity.SysUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author jiaok
 * @Date 2023/5/11 10:12
 */

@Component("ex")
public class CustomSecurityExpressionRoot {

    public boolean hasAuthority(String authority){

//        获取内存中的权限信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        SysUser sysUser = (SysUser) authentication.getPrincipal();
        List<GrantedAuthority> permissions = sysUser.getGrantedAuthorities();
        return permissions.contains(new SimpleGrantedAuthority(authority));
    }

}
